package com.lyw.enums;

import com.google.common.collect.Maps;
import com.lyw.utils.FileUtils;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public enum DuplicateEnum {

  // 1234 上右下左
  JI_JING_CITY(
          new Object[]{"1-3", new int[]{1, 2}, "1-2", new int[]{2, 2}, "2-2", new int[]{2, 1},
                  "2-1", new int[]{2, 0}, "2-0", new int[]{1, 0, 3, 0}, "3-0", new int[]{4, 0},
                  "1-0", new int[]{2, 0}, "1-1", new int[]{2, 1}, "3-2", new int[]{2, 2},
                  "3-1", new int[]{3, 0}, "4-0", new int[]{-1, -1}},
          new int[]{1, 3},
          new int[]{4, 0},
          Arrays.asList(new int[]{1, 3}, new int[]{1, 2}, new int[]{2, 2}, new int[]{2, 1},
                  new int[]{2, 0}, new int[]{3, 0}, new int[]{4, 0}),
          new int[]{5, 4},
          DuplicateNameEnum.JJC,
          "320-255",
          "50-65-100",
          new DirectionEnum[]{
                  DirectionEnum.TOP,
                  DirectionEnum.RIGHT,
                  DirectionEnum.TOP,
                  DirectionEnum.TOP,
                  DirectionEnum.RIGHT,
                  DirectionEnum.RIGHT
          },
          PositionEnum.BOTTOM_LEFT,
          18);

  // 副本定位
  private String position;
  // 定位的颜色
  private String color;
  private DirectionEnum[] route;
  private PositionEnum positionEnum;
  private int[] mapCount;
  private int mapSize;
  private DuplicateNameEnum duplicateName;
  private int[] endRoomPosition;
  private int[] startRoomPosition;
  private int[] mini_map;
  private List<int[]> duplicate_route;
  private Map<String, int[]> duplicate_route_container;
  private Map<int[], int[]> back_route;
  private Map<int[], Integer> cur_clearance_count;

  public static int[] equals_val;

  DuplicateEnum(Object[] duplicate_route_container, int[] startRoomPosition, int[] endRoomPosition, List<int[]> duplicate_route, int[] mini_map, DuplicateNameEnum duplicateName, String position, String color, DirectionEnum[] route, PositionEnum positionEnum, int mapSize) {
    this.duplicate_route_container = new HashMap<>();
    for (int i = 0; i < duplicate_route_container.length; i += 2) {
      String key = (String) duplicate_route_container[i];
      int[] value = (int[]) duplicate_route_container[i + 1];
      this.duplicate_route_container.put(key, value);
    }
    this.startRoomPosition = startRoomPosition;
    this.endRoomPosition = endRoomPosition;
    this.mini_map = mini_map;
    this.duplicateName = duplicateName;
    this.position = position;
    this.color = color;
    this.route = route;
    this.positionEnum = positionEnum;
    this.mapSize = mapSize;
    this.duplicate_route = duplicate_route;
  }

  public static Map<String, int[]> put(DuplicateEnum duplicateEnum, String key, int[] value) {
    duplicateEnum.duplicate_route_container.put(key, value);
    return duplicateEnum.duplicate_route_container;
  }

  public Map<int[], int[]> getBack_route() {
    return back_route;
  }

  public static boolean equals(Map<int[], int[]> back_route, int[] argument) {
    List<int[]> collect = back_route.keySet().stream().filter(item -> item[0] == argument[0] && item[1] == argument[1])
            .collect(Collectors.toList());
    if (collect.size() > 0) {
      equals_val = collect.get(0);
      return true;
    }
    return false;
  }


  public int getCur_clearance_count(int[] cur_room_position) {
    for (int[] ints : cur_clearance_count.keySet()) {

      if (ints[0] == cur_room_position[0] && ints[1] == cur_room_position[1]) {
        return cur_clearance_count.get(ints);
      }
    }
    return 0;
  }

  public Map<String, int[]> getDuplicate_route_container() {
    return duplicate_route_container;
  }

  public void setDuplicate_route_container(Map<String, int[]> duplicate_route_container) {
    this.duplicate_route_container = duplicate_route_container;
  }

  public void setCur_clearance_count(Map<int[], Integer> cur_clearance_count) {
    this.cur_clearance_count = cur_clearance_count;
  }

  public void setBack_route(Map<int[], int[]> back_route) {
    this.back_route = back_route;
  }

  public List<int[]> getDuplicate_route() {
    return duplicate_route;
  }

  public void setDuplicate_route(List<int[]> duplicate_route) {
    this.duplicate_route = duplicate_route;
  }

  public int[] getMini_map() {
    return mini_map;
  }

  public void setMini_map(int[] mini_map) {
    this.mini_map = mini_map;
  }

  public DuplicateNameEnum getDuplicateName() {
    return duplicateName;
  }

  public void setDuplicateName(DuplicateNameEnum duplicateName) {
    this.duplicateName = duplicateName;
  }

  public int getMapSize() {
    return mapSize;
  }

  public void setMapSize(int mapSize) {
    this.mapSize = mapSize;
  }

  public int[] getMapCount() {
    return mapCount;
  }

  public void setMapCount(int[] mapCount) {
    this.mapCount = mapCount;
  }

  public PositionEnum getPositionEnum() {
    return positionEnum;
  }

  public void setPositionEnum(PositionEnum positionEnum) {
    this.positionEnum = positionEnum;
  }

  public String getPosition() {
    return position;
  }

  public void setPosition(String position) {
    this.position = position;
  }

  public String getColor() {
    return color;
  }

  public void setColor(String color) {
    this.color = color;
  }

  public DirectionEnum[] getRoute() {
    return route;
  }

  public void setRoute(DirectionEnum[] route) {
    this.route = route;
  }

  // 获取下一个房间的坐标
  public int[] getNextRoomPosition(int[] cur_position) throws IOException {

    int[] ints = duplicate_route_container.get(cur_position[0] + "-" + cur_position[1]);

    if (ints == null) {
      return new int[]{-1, -1};
    }

    if (ints.length == 4) {
      List<int[]> clearanceRoomPosition = FileUtils.getClearanceRoomPosition();
      for (int[] ints1 : clearanceRoomPosition) {
        if (ints1[0] == ints[0] && ints1[1] == ints[1]) {
          return new int[]{ints[2], ints[3]};
        }
      }
      return new int[]{ints[0], ints[1]};
    }

    return ints;

    /*if (cur_position[0] == 4 && cur_position[1] == 0) {
      return new int[]{-1, -1};
    }

    if (cur_position[0] == 2 && cur_position[1] == 0) {
      List<int[]> clearanceRoomPosition = FileUtils.getClearanceRoomPosition();
      for (int[] ints : clearanceRoomPosition) {
        if (ints[0] == 1 && ints[1] == 0) {
          return new int[]{3, 0};
        }
      }
      return new int[]{1, 0};
    }
    for (int i = 0; i < duplicate_route.size(); i++) {
      int[] ints = duplicate_route.get(i);
      if (ints[0] == cur_position[0] && ints[1] == cur_position[1]) {
        return duplicate_route.get(++i);
      }
    }

    // 备份地图
    if (DuplicateEnum.equals(back_route, cur_position)) {
      return back_route.get(DuplicateEnum.equals_val);
    }

    // 结束了
    return new int[]{-1, -1};*/
  }

  /**
   * 加载副本的备份地图信息
   */
  public static void load_mini_map() {

    Map<int[], int[]> ji_jing_cheng = new HashMap<>();
    ji_jing_cheng.put(new int[]{1, 0}, new int[]{2, 0});
    ji_jing_cheng.put(new int[]{1, 1}, new int[]{2, 1});
    ji_jing_cheng.put(new int[]{2, 0}, new int[]{1, 0});
    ji_jing_cheng.put(new int[]{3, 2}, new int[]{2, 2});
    ji_jing_cheng.put(new int[]{3, 1}, new int[]{3, 0});

    Map<int[], Integer> room_clearance_count = new HashMap<>();
//    room_clearance_count.put(new int[]{1, 0}, 5);
//    room_clearance_count.put(new int[]{1, 1}, 2);
//    room_clearance_count.put(new int[]{3, 2}, );
//    room_clearance_count.put(new int[]{3, 1}, );
    room_clearance_count.put(new int[]{1, 3}, 0);
    room_clearance_count.put(new int[]{1, 2}, 1);
    room_clearance_count.put(new int[]{2, 2}, 2);
    room_clearance_count.put(new int[]{2, 1}, 3);
    room_clearance_count.put(new int[]{2, 0}, 4);
    room_clearance_count.put(new int[]{3, 0}, 5);
    room_clearance_count.put(new int[]{4, 0}, 6);

    DuplicateEnum.JI_JING_CITY.setBack_route(ji_jing_cheng);
    DuplicateEnum.JI_JING_CITY.setCur_clearance_count(room_clearance_count);
  }

  public int[] getEndRoomPosition() {
    return endRoomPosition;
  }

  public int[] getStartRoomPosition() {
    return startRoomPosition;
  }
}
